import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useTagsViewStore = defineStore('tagsView', () => {
  const visitedViews = ref([])

  const addVisitedView = (view) => {
    if (visitedViews.value.some(v => v.path === view.path)) return
    visitedViews.value.push({
      title: view.meta?.title || view.name || view.path,
      path: view.path,
      affix: view.meta?.affix === true
    })
  }

  const delVisitedView = (view) => {
    const index = visitedViews.value.findIndex(v => v.path === view.path)
    if (index !== -1) visitedViews.value.splice(index, 1)
  }

  const delOthersViews = (currentPath) => {
    visitedViews.value = visitedViews.value.filter(v => v.affix || v.path === currentPath)
  }

  const delAllViews = () => {
    visitedViews.value = visitedViews.value.filter(v => v.affix)
  }

  return {
    visitedViews,
    addVisitedView,
    delVisitedView,
    delOthersViews,
    delAllViews
  }
})


